package com.example.majiang.service;

import com.example.majiang.dto.NotificationDTO;
import com.example.majiang.dto.PaginationDTO;
import com.example.majiang.dto.QuesitionDTO;
import com.example.majiang.enums.NotificationStatusEnum;
import com.example.majiang.enums.NotificationTypeEnum;
import com.example.majiang.exception.CustomizeErrorCode;
import com.example.majiang.exception.CustomizeException;
import com.example.majiang.mapper.NotificationMapper;
import com.example.majiang.model.Notification;
import com.example.majiang.model.Question;
import com.example.majiang.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class NotificationService {
    @Autowired
    private NotificationMapper notificationMapper;

    public PaginationDTO list(Integer userId, Integer page, Integer size) {

        PaginationDTO paginationDTO = new PaginationDTO();

        Integer totalPage;

        Integer totalCount = notificationMapper.CountByReceiver(userId);

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }

        paginationDTO.setPagination(totalPage, page);
        List<Notification> notifications=null;

        if(page==0){
            Integer offset = size * page;
            notifications = notificationMapper.listByUserId(userId,offset,size);

        }
        else{
            Integer offset = size * (page - 1);
            notifications = notificationMapper.listByUserId(userId, offset, size);

        }

        //size*(page-1)
        List<NotificationDTO> notificationDTOS = new ArrayList<>();


        for (Notification notification : notifications) {
            NotificationDTO notificationDTO = new NotificationDTO();
            BeanUtils.copyProperties(notification, notificationDTO);
            notificationDTO.setTypeName(NotificationTypeEnum.nameOfType(notification.getType()));
            notificationDTOS.add(notificationDTO);
        }
        paginationDTO.setData(notificationDTOS);
        return paginationDTO;
    }
    public Integer unreadCount(Integer userId) {
        Integer status=NotificationStatusEnum.UNREAD.getStatus();
       return notificationMapper.countUnRead(userId,status);
    }
    public NotificationDTO read(Integer id, User user){
        Notification notification=notificationMapper.selectById(id);
        if(notification==null){
            throw new CustomizeException(CustomizeErrorCode.NOTIFICATION_NOT_FOUND);
        }
        if(!Objects.equals(notification.getReceiver(),user.getId()))
        {
            throw new CustomizeException(CustomizeErrorCode.READ_NOTIFICATION_FAIL);
        }
        notificationMapper.updateRead(NotificationStatusEnum.READ.getStatus(), id);
        NotificationDTO notificationDTO = new NotificationDTO();
        BeanUtils.copyProperties(notification, notificationDTO);
        notificationDTO.setTypeName(NotificationTypeEnum.nameOfType(notification.getType()));
        return notificationDTO;
    }

}
